import { createApp, type App as VueApp } from "vue";
import {
    renderWithQiankun,
    qiankunWindow,
    type QiankunProps,
} from "vite-plugin-qiankun/dist/helper";
// @ts-ignore
import App from "./App.vue";

let app: VueApp<Element>;

function renderApp(container: any) {
    app = createApp(App);
    app.mount(container);
}

if (qiankunWindow.__POWERED_BY_QIANKUN__) {
    renderWithQiankun({ bootstrap, mount, unmount, update });
} else {
    renderApp("#app");
}

async function bootstrap() {
    console.log("vue3 app bootstrap");
}

async function mount(props: QiankunProps) {
    console.log("子应用2挂载，主应用传递参数：", {
        nickName: props.nickName,
        age: props.age
    });
    localStorage.setItem('userInfo', JSON.stringify({
        nickName: props.nickName,
        age: props.age
    }))
    renderApp(props.container?.querySelector("#app"));
}

async function unmount() {
    app?.unmount();
}

async function update() {
    console.log("vue3 app update");
}